package controllers;

import beans.AlbumBean;
import beans.ArticleBean;
import beans.PageBean;
import beans.WebServiceBean;
import models.Album;
import models.Article;
import models.Collection;
import play.mvc.Controller;
import play.mvc.With;
import utils.SessionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xieyicheng on 2015/2/4.
 */
@With(value = CheckLogin.class)
public class Collections extends Controller {
    public static void collectWork(int page) {
        String userId = SessionUtils.getUserId();
        long total = Collection.count("owner_id = ? and type = ?",userId,Collection.WORK);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Collection> collectionList = Collection.find(
                "owner_id = ? and type = ? order by createTime desc",userId,Collection.WORK).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        List<Article> articleList = new ArrayList<Article>();
        for(int i = 0;i < collectionList.size();i++)
        {
            Article article = Article.findById(collectionList.get(i).collect_id);
            if(article == null)
            {
                continue;
            }
            articleList.add(article);
        }
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = ArticleBean.buildList(articleList);
        render(webServiceBean,pageBean);
    }
    public static void collectAlbum(int page) {
        String userId = SessionUtils.getUserId();
        long total = Collection.count("owner_id = ? and type = ?", userId, Collection.ALBUM);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Collection> collectionList = Collection.find(
                "owner_id = ? and type = ? order by createTime desc",userId,Collection.ALBUM).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        List<Album> albumList = new ArrayList<Album>();
        for(int i = 0;i < collectionList.size();i++)
        {
            Album album = Album.findById(collectionList.get(i).collect_id);
            if(album == null)
            {
                continue;
            }
            albumList.add(album);
        }
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = AlbumBean.bulidList(albumList);
        //renderJSON(webServiceBean);
        render(webServiceBean,pageBean);
    }
}
